package org.groupg.practice.guiPractice.awt;

import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.List;

public class MacroRecorder {
    private static List<Action> actions = new ArrayList<>();
    private static boolean recording = false;

    public static void main(String[] args) throws AWTException {
        // 设置全局快捷键监听
        KeyboardFocusManager.getCurrentKeyboardFocusManager()
            .addKeyEventDispatcher(e -> {
                if (e.getID() == KeyEvent.KEY_PRESSED && e.getKeyCode() == KeyEvent.VK_F12) {
                    recording = !recording;
                    System.out.println("Recording: " + recording);
                    return true;
                }
                return false;
            });

        Robot robot = new Robot();

        // 添加鼠标监听
        MouseListener mouseListener = new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                if (recording) {
                    actions.add(new MouseAction(e.getX(), e.getY(), true));
                }
            }
            public void mouseReleased(MouseEvent e) {
                if (recording) {
                    actions.add(new MouseAction(e.getX(), e.getY(), false));
                }
            }
        };

        // 添加键盘监听
        KeyListener keyListener = new KeyAdapter() {
            public void keyPressed(KeyEvent e) {
                if (recording) {
                    actions.add(new KeyAction(e.getKeyCode(), true));
                }
            }
            public void keyReleased(KeyEvent e) {
                if (recording) {
                    actions.add(new KeyAction(e.getKeyCode(), false));
                }
            }
        };

        // 回放功能
        new Thread(() -> {
            while (true) {
                if (!recording && !actions.isEmpty()) {
                    for (Action action : actions) {
                        action.execute(robot);
                        try {
                            Thread.sleep(50); // 适当延迟
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }).start();
    }
}

interface Action {
    void execute(Robot robot);
}

class MouseAction implements Action {
    private int x, y;
    private boolean press;

    MouseAction(int x, int y, boolean press) {
        this.x = x;
        this.y = y;
        this.press = press;
    }

    public void execute(Robot robot) {
        robot.mouseMove(x, y);
        if (press) {
            robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
        } else {
            robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
        }
    }
}

class KeyAction implements Action {
    private int keyCode;
    private boolean press;

    KeyAction(int keyCode, boolean press) {
        this.keyCode = keyCode;
        this.press = press;
    }

    public void execute(Robot robot) {
        if (press) {
            robot.keyPress(keyCode);
        } else {
            robot.keyRelease(keyCode);
        }
    }
}